import { reqGoodDetailInfoData } from '@/api'

const state = {
  goodDetailInfo: {},
}
const actions = {
  async getGoodDetailInfoData({ commit }, id) {
    const result = await reqGoodDetailInfoData(id)
    if (result.code === 200) {
      commit('SAVE_GOOD_DETAIL_INFO_DATA', result.data)
    } else {
      console.log(result.message)
    }
  },
}
const mutations = {
  SAVE_GOOD_DETAIL_INFO_DATA(state, payload) {
    state.goodDetailInfo = payload
  },
  UPDATE_GOOD_PARAM_CHECKED(state, obj) {
    let { spuSaleAttrValue, spuSaleAttr } = obj
    spuSaleAttr.spuSaleAttrValueList.forEach((item) => (item.isChecked = '0'))
    spuSaleAttrValue.isChecked = spuSaleAttrValue.isChecked === '1' ? '0' : '1'
  },
  UPDATE_DETAIL_SKU_DEFAULT_IMG(state,url){
    state.goodDetailInfo.skuInfo.skuDefaultImg = url
  }
}

const getters = {
  categoryView(state) {
    const categoryView = state.goodDetailInfo.categoryView
    return categoryView ? categoryView : {}
  },
  skuInfo(state) {
    const skuInfo = state.goodDetailInfo.skuInfo
    return skuInfo ? skuInfo : {}
  },
  spuSaleAttrList(state) {
    const spuSaleAttrList = state.goodDetailInfo.spuSaleAttrList
    return spuSaleAttrList ? spuSaleAttrList : []
  },
}
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
}
